<?php
/**
 * location element class
 *
 * @final
 * @author raphael seebacher <raphasee@ee.ethz.ch>
 * @version 0.01
 */
final class location extends message_element_controller {
    public function get_tag() {
        return 'location';
    }

    protected function get_location_report($raw_lr_data) {
        $location_report = new location_report();
        $location_report->set_all($raw_lr_data);

        return $location_report;
    }

    protected function process_element() {
        $lr_ok_counter = 0;
        $lr_total = count($this->content);
        $uc = user_controller::get_instance();
        $user_id = $uc->get_user()->get_id();

        foreach ($this->content as $raw_lr_data) {
            try {
                $raw_lr_data['user_id'] = $user_id;
                $lr = $this->get_location_report($raw_lr_data);
                $lr->save();
                $lr_ok_counter++;
            }
            catch (model_exception $e) {

            }
        }

        if ($lr_ok_counter == 0) {
            throw new error('No location reports could be parsed.');
        }
        else if ($lr_ok_counter < $lr_total) {
            throw new warning('Only '.$lr_ok_counter.' out of '.$lr_total
                              .' location reports could be parsed.'
            );
        }

        return Array(
          'status' => 'ok',
        );
    }
}
?>